package com.city58
{
	import com.base.data.account.AccountBean;
	import com.base.event.CustomEvent;
	import com.base.log.Log;
	import com.base.login.Login;
	import com.base.properties.Properties;
	import com.base.properties.PropertiesManmger;
	import com.base.ui.other.StatusWindow;
	import com.base.ui.other.VerificationCodeWindow;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.html.HTMLLoader;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	public class City58Login extends Login
	{
		public var account:AccountBean;
		
		public var success:Function;
		
		public var error:Function;
		
		public var isAutoQuit:Boolean;
		
		public var validateCode:String;
		
		private var that:*;
		
		private var ptk:String;
		
		private var cd:String;
		
		private var p1:String;
		
		private var p2:String;
		
		private var p3:String;
		
		private var step:uint;
		
		public var content:String;
		
		public function City58Login()
		{
			super();
			that = this;
		}
		
		override public function login(account:AccountBean, success:Function, error:Function, isAutoQuit:Boolean = true):Login {
			super.login( account ,success ,error ,isAutoQuit );
			
			this.account = account;
			this.success = success;
			this.error = error;
				
			var load:HTMLLoader = new HTMLLoader();
			load.cacheResponse = false;
			load.placeLoadStringContentInApplicationSandbox = true;
			load.addEventListener( Event.COMPLETE , onComplete );
			load.addEventListener( IOErrorEvent.IO_ERROR ,onError );
			load.load( new URLRequest( PropertiesManmger.instance.getCity58Cfg().getProperty("loginPageUrl") ) );
			
			
			return this;
		}
		
		private function loadLogin():void {
			var s:String = "<html>"
				+"<head>"
				+"<script type='text/javascript' src='cfg/ppt_security.js'></script>"
				+"<script type='text/javascript' src='cfg/58login.js'></script>"
				+"</head>"
				+"<body>"
				+"</body>"
				+"</html>";
			var load:HTMLLoader = new HTMLLoader();
			load.cacheResponse = false;
			load.placeLoadStringContentInApplicationSandbox = true;
			load.addEventListener( Event.COMPLETE , onComplete );
			load.addEventListener( IOErrorEvent.IO_ERROR ,onError );
			load.loadString( s );
		}
		
		private function startLogin():void {
			
			var request:URLRequest = new URLRequest(  PropertiesManmger.instance.getCity58Cfg().getProperty("loginUrl") );
			request.method = URLRequestMethod.POST;
			
			var variables:URLVariables = new URLVariables();
			variables.cd = cd;
			variables.isweak = 0;
			variables.p1 = p1;
			variables.p2 = p2;
			variables.p3 = p3;
			variables.password = "password";
			variables.path = "";
			variables.ptk = ptk;
			variables.timesign = 1367828431953;
			variables.username = account.userName;
			if( validateCode != null ) variables.validatecode = validateCode;
			request.data = variables;
			
			var load:URLLoader = new URLLoader();
			load.load( request );
			load.addEventListener( Event.COMPLETE , onComplete );
			load.addEventListener( IOErrorEvent.IO_ERROR ,onError );
		}
		
		override public function logout():Login {
			StatusWindow.addText("用户名："+account.userName+"退出登录");
			var load:URLLoader = new URLLoader();
			load.load(  new URLRequest( PropertiesManmger.instance.getCity58Cfg().getProperty("logoutUrl") ) );
			return this;
		}
		
		private function veryfyCallBack( val:* ):void {
			this.validateCode = val;
			startLogin();
		}
		
		
		private function onComplete( e:Event ):void {
			EventDispatcher(e.target).removeEventListener( IOErrorEvent.IO_ERROR , onError );
			EventDispatcher(e.target).removeEventListener( e.type , onComplete );
			
			switch( step )
			{
				case 0:
					if( HTMLLoader(e.target).location.indexOf("http://my.58.com/") != -1 ) {
						if( isAutoQuit ) logout();
						success( this );
						return;
					}
					ptk = e.target.window.document.getElementById("ptk").value;
					cd = e.target.window.document.getElementById("cd").value ;
					Log.log("登录获取 ptk = "+ptk+" cd = "+cd );
					step ++;
					loadLogin();
					
					break;
				case 1:
					var pArr:Array = e.target.window.setPostValue( account.password );
					p1 = pArr[0]; p2 = pArr[1]; p3 = pArr[2];
					Log.log("登录获取 p1 = "+p1+" p2 = "+p2+" p3 = "+p3 );
					step ++;
					startLogin();
					break;
				case 2:
					content = URLLoader(e.target).data as String;
					Log.log("58登录响应",content );
					var i:int = String(content).indexOf("验证码");
					if( i != -1 ) { //弹出验证码窗口
						new VerificationCodeWindow().show(  PropertiesManmger.instance.getCity58Cfg() , veryfyCallBack );
						return;
					}
					
					i = String( content ).indexOf("您输入的密码与账户名不符");
					if( i != -1 ) {
						error( this );
						return;
					}
					
					if( isAutoQuit ) logout();
					success( this );
					break;
				default:
					break;
			}
			
		}
		
		private function onError( e:Event ):void {
			EventDispatcher(e.target).removeEventListener(  Event.COMPLETE , onComplete );
			EventDispatcher(e.target).removeEventListener( e.type , onError );
			content = "58登录出现网络错误";
			Log.log( "58登录出现网络错误", e.toString() );
			error ( this );
		}
		
	}
}